﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using System.Web.UI.WebControls;
using SmartProject.Data;

namespace SmartProject.BusinessObjects
{
    public class Product : Entity
    {
        public Product()
        {
            DataParameter[] dataParameters = new DataParameter[5];
            dataParameters[0] = new DataParameter("ProductId", DataType.Integer);
            dataParameters[1] = new DataParameter("Description", 150);
            dataParameters[2] = new DataParameter("Created", DataType.DateTime);
            dataParameters[3] = new DataParameter("Discontinued", DataType.Boolean);
            dataParameters[4] = new DataParameter("OwnerId", DataType.Integer);
            base.Initialize(dataParameters);
        }

        public void Populate(DropDownList dropDownList)
        {
            dropDownList.Items.Clear();
            DataTable products = LoadProducts();
            if (products != null)
            {
                foreach (DataRow row in products.Rows)
                {
                    int productId = (int)row["ProductId"];
                    dropDownList.Items.Add(new ListItem(row["Description"].ToString(),
                        productId.ToString()));
                }
            }
        }

        public DataTable LoadProducts()
        {
            return base.Find("smartProject_GetProducts");
        }

        public override void Load()
        {
            base.Load("smartProject_GetProduct");
        }

        public override void Save()
        {
            base.Load("smartProject_AddProduct");
        }

        public void Discontinue()
        {
            base.Run("smartProject_UpdateProductDiscontinued");
        }

        public override void Update()
        {
            base.Run("smartproject_UpdateProduct");
        }

        public override void Remove()
        {
            base.Run("smartProject_RemoveProduct");
        }
    }
}
